package com.easy.dfc.service;

import com.easy.common.bean.Result;
import com.easy.common.constant.EnumConfig;
import com.easy.dfc.entity.AskOther;
import com.easy.dfc.entity.ParaFactoryProducts;
import com.easy.dfc.mapper.AskOtherMapper;
import com.easy.dfc.mapper.DfcAdvertisementMapper;
import com.easy.dfc.mapper.FactorySetMapper;
import com.easy.dfc.mapper.ProductsetMapper;
import net.sf.cglib.beans.BeanMap;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FactoryIndexService {

    @Resource
    private DfcAdvertisementMapper dfcAdvertisementMapper;

    @Resource
    private AskOtherMapper askOtherMapper;

    @Resource
    private ProductsetMapper productsetMapper;

    @Resource
    private FactorySetMapper factorySetMapper;

    public Result HomePage() {
        Integer askSize = 5; //风车话题前五
        Integer factorySize = 2;//好厂推荐 前4

        List<Map<String,Object>> bannerList = dfcAdvertisementMapper.getHomeBanner();////顶部轮播

        List<AskOther> askOtherList = askOtherMapper.getFactoryAskOther(askSize);//风车话题前五
        List<Map<String,Object>> banner1List = dfcAdvertisementMapper.getMoreBanner(80);////中部
        List<Map<String,Object>> niceFactoryList = factorySetMapper.getNiceFactoryList(factorySize);//好厂推荐，订单最多的店铺

        Result rt = new Result();
        Map<String,Object> map = new HashMap<String,Object>();
        if(bannerList == null)bannerList = new ArrayList();
        if(askOtherList == null)askOtherList = new ArrayList();
        if(banner1List == null)banner1List = new ArrayList();
        if(niceFactoryList == null)niceFactoryList = new ArrayList();

        map.put("banner",bannerList);
        map.put("banner1",banner1List);
        map.put("askOther",askOtherList);
        List list = new ArrayList();
        for(int i =0; i < niceFactoryList.size(); i++){
            Map<String,Object> factory = niceFactoryList.get(i);
            List<Map<String,Object>> productList = factorySetMapper.getNiceProductByFactoryId((Integer)factory.get("factoryId"));
            factory.put("productList",productList);
            list.add(factory);
        }
        map.put("niceFactory",list);
        rt.setStatus(EnumConfig.S_CODE.OK);
        rt.setData(map);
        return rt;

    }

    public Result getCateories(Integer typeId) {
        Result rt = new Result();
        List<Map<String,Object>> niceProductList = factorySetMapper.getCateories(typeId);
        if(niceProductList == null)niceProductList = new ArrayList();
        rt.setStatus(EnumConfig.S_CODE.OK);
        rt.setData(niceProductList);
        return rt;
    }

    public Result getProducts(ParaFactoryProducts para) {
        Result rt = new Result();
        List<Map<String,Object>> niceProductList = factorySetMapper.getProducts(para);
        if(niceProductList == null)niceProductList = new ArrayList();
        rt.setStatus(EnumConfig.S_CODE.OK);
        rt.setData(niceProductList);
        return rt;
    }
}
